package com.starling.control.file
{
	import com.starling.core.InfoButton;
	import com.starling.core.InfoImage;
	import com.starling.core.InfoMovieClip;
	import com.starling.core.InfoScale9Grid;
	import com.starling.core.InfoSprite;
	import com.starling.utils.TextureUtil;
	
	import flash.display.DisplayObject;
	import flash.display.Loader;
	import flash.events.Event;
	import flash.net.URLRequest;
	import flash.system.LoaderContext;
	import flash.system.System;

	public class FileSWF extends FileBase
	{
		private var _loader:Loader;
		
		public function FileSWF(path:String=null)
		{
			super(path);
		}
		
		
		/**
		 * 加载 
		 * 
		 */	
		override public function loadFile(callback:Function):void
		{
			super.loadFile(callback);
			
			dispose();
			fileItems = new Array();
			
			var loaderContext:LoaderContext = new LoaderContext(); 
			loaderContext.allowLoadBytesCodeExecution = true; 
			_loader = new Loader();
			_loader.contentLoaderInfo.addEventListener(Event.COMPLETE ,loadComplete);
			_loader.load(new URLRequest(this.nativePath) ,loaderContext);
		}
		
		
		/**
		 * 加载完成 
		 * 
		 */
		private function loadComplete(event:Event):void
		{
			var classItems:Vector.<String> = _loader.content.loaderInfo.applicationDomain.getQualifiedDefinitionNames();
			
			for (var i:int=0;i<classItems.length;i++)
			{
				var display:DisplayObject = new (_loader.content.loaderInfo.applicationDomain.getDefinition(classItems[i]) as Class);
				var textureName:String = TextureUtil.getClassName(display);
				var textureType:String = TextureUtil.getClassType(textureName);
				fileItems.push({display:display ,textureName:textureName ,textureType:textureType});
				//trace(textureName ,textureType);
			}
			
			this._fileSize = this.size;
			this._fileNum = 1;
			this._callback();
		}
		
		
		
		/**
		 * 显示对象数据 
		 * @param scale
		 * @return 
		 * 
		 */		
		override public function getInfo(scale:Number):Object
		{
			var info:Object = {};
			for (var i:int=0;i<fileItems.length;i++)
			{
				switch(fileItems[i].textureType)
				{
					case TextureUtil.TYPE_IMAGE:
						info[fileItems[i].textureName] = InfoImage.getInfo(fileItems[i].display ,scale);
						break;
					
					case TextureUtil.TYPE_MOVIECLIP:
						info[fileItems[i].textureName] = InfoMovieClip.getInfo(fileItems[i].display ,scale);
						break;
					
					case TextureUtil.TYPE_SCALE9GRID:
						info[fileItems[i].textureName] = InfoScale9Grid.getInfo(fileItems[i].display ,scale);
						break;
					
					case TextureUtil.TYPE_SPRITE:
						info[fileItems[i].textureName] = InfoSprite.getInfo(fileItems[i].display ,scale);
						break;
					
					case TextureUtil.TYPE_BUTTON:
						info[fileItems[i].textureName] = InfoButton.getInfo(fileItems[i].display ,scale);
						break;
				}
			}
			
			return info;
		}
		
		
		
		/**
		 * 回收 
		 * 
		 */		
		override public function dispose():void
		{
			if(_loader!=null)
			{
				_loader.contentLoaderInfo.removeEventListener(Event.COMPLETE ,loadComplete);
				_loader.unloadAndStop();
				_loader = null;
			}
			
			if(fileItems!=null)
			{
				for (var i:int=0;i<fileItems.length;i++)
					fileItems[i] = null;
				fileItems = null;
			}
			
			System.gc();
		}
	}
}